package cn.fl.system.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import cmm.comm.facade.framework.api.ISysDictionaryApiService;
import cmm.comm.facade.framework.dto.SysDictionaryDTO;
import cmm.mid.core.framework.page.DataResultVO;
import cmm.mid.core.framework.page.KeyValueVO;
import cmm.mid.core.framework.page.ListPageVO;
import cmm.mid.core.framework.page.Pager;
import cmm.user.facade.system.api.ISysOrgApiService;
import cmm.web.core.framework.controller.BaseController;
import cn.cmm.base.framework.constant.BaseConstant;
import cn.cmm.base.framework.constant.BaseExceptionConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.framework.constant.DictionaryConstant;
import cn.fl.system.condition.SysScoreIdxOptQueryCondition;
import cn.fl.system.condition.SysScoreIdxQueryCondition;
import cn.fl.system.condition.SysScoreTmplQueryCondition;
import cn.fl.system.constant.SysLogConstant;
import cn.fl.system.dto.SysScoreTmplDTO;
import cn.fl.system.service.ISysScoreIdxOptService;
import cn.fl.system.service.ISysScoreIdxService;
import cn.fl.system.service.ISysScoreTmplService;
import cn.fl.system.transform.SysScoreTmplVOTransform;
import cn.fl.system.vo.SysScoreIdxOptVO;
import cn.fl.system.vo.SysScoreIdxVO;
import cn.fl.system.vo.SysScoreTmplVO;
import net.sf.json.JSONArray;

/**
 * @Description: 评分模板表 控制层
 */
@Controller
@RequestMapping("/system/sysscoretmpl")
public class SysScoreTmplController extends BaseController {

	private static final Logger logger = LoggerFactory.getLogger(SysScoreTmplController.class);

	/**
	 * SysScoreTmpl 服务层BEAN
	 */
	@Autowired
	private ISysScoreTmplService sysScoreTmplService;
	
	@Autowired
	private ISysScoreIdxService sysScoreIdxService;
	
	@Autowired
	private ISysScoreIdxOptService sysScoreIdxOptService;
	/**
	 * 字典服务层BEAN
	 */
	@Autowired
	private ISysDictionaryApiService sysDictionaryApiService;
	
	@Autowired
	private ISysOrgApiService sysOrgApiService;
	/**
	 * 到列表页面
	 */
	@RequestMapping(value = "/toSysscoretmplListPage.do", method = { RequestMethod.GET})
	public ModelAndView toListPage(HttpServletRequest request, HttpServletResponse response) {
		// 事业部
		List<KeyValueVO> orgList = this.sysScoreTmplService.searchBusinessOrgList();
		
		logger.info("加载SysScoreTmpl列表页面.");
		ModelAndView view = new ModelAndView("system/sysscoretmpl/sysScoreTmplInfoList");
		view.addObject("orgListList", orgList);
		return view;
	}
	
	@RequestMapping(value = "/toSysscoreIdxListPage.do", method = { RequestMethod.GET})
	public ModelAndView toSysscoreIdxListPage(HttpServletRequest request, HttpServletResponse response) {
		// 事业部
	//	List<KeyValueVO> orgList = this.sysScoreTmplService.searchBusinessOrgList();
		String tmplId=request.getParameter("tmplId");
		logger.info("加载SysScoreIdx列表页面.");
		ModelAndView view = new ModelAndView("system/sysscoretmpl/sysScoreIdxInfoList");
		view.addObject("tmplId", tmplId);
		return view;
	}

	@RequestMapping(value = "/toSysscoreIdxOptListPage.do", method = { RequestMethod.GET})
	public ModelAndView toSysscoreIdxOptListPage(HttpServletRequest request, HttpServletResponse response) {
		// 事业部
	//	List<KeyValueVO> orgList = this.sysScoreTmplService.searchBusinessOrgList();
		String idxId=request.getParameter("idxId");
		logger.info("加载SysScoreIdxOpt列表页面.");
		ModelAndView view = new ModelAndView("system/sysscoretmpl/sysScoreIdxOptInfoList");
		view.addObject("idxId", idxId);
		return view;
	}
	/**
	 * 到修改页面
	 */
	@RequestMapping(value = "/toModifyPage.do", method = { RequestMethod.GET, RequestMethod.POST })
	public ModelAndView toModifyPage(HttpServletRequest request, HttpServletResponse response) {
		String id = request.getParameter("Tmplid");
		String detailFlag = request.getParameter("detailFlag");
		if(detailFlag==null){
			detailFlag="true";
		}else{
			detailFlag="false";
		}
		SysScoreTmplQueryCondition qc = new SysScoreTmplQueryCondition();
		qc.setId(id);
		Pager page = new Pager();
    	page.setCondition(qc);
		page= this.sysScoreTmplService.searchListPage(page);
		List<SysScoreTmplDTO> list = (List<SysScoreTmplDTO>) page.getResultList();
		SysScoreTmplDTO tmplDTO=list.get(0);
		logger.info("加载SysScoreTmpl Modify页面.");
		ModelAndView view = new ModelAndView("system/sysscoretmpl/sysscoretmplAdd");
		view.addObject("tmplId", id);
		view.addObject("tmplNm", tmplDTO.getTmplNm());
		view.addObject("tmplCd", tmplDTO.getTmplCd());
		view.addObject("orgCd", tmplDTO.getOrgCd());
		view.addObject("cons", tmplDTO.getCons());
		view.addObject("orgNm", this.sysOrgApiService.selectSysOrgNmByOrgCd(tmplDTO.getOrgCd(), tmplDTO.getSysCd()));
		view.addObject("detailFlag", detailFlag);
		return view;
	}
	
	@RequestMapping(value = "/toIdxModifyPage.do", method = { RequestMethod.GET, RequestMethod.POST })
	public ModelAndView toModifyIdxPage(HttpServletRequest request, HttpServletResponse response) {
		String detailFlag = request.getParameter("detailFlag");
		if(detailFlag==null){
			detailFlag="true";
		}else{
			detailFlag="false";
		}
		String id = request.getParameter("idxId");
		SysScoreIdxQueryCondition qc = new SysScoreIdxQueryCondition();

		qc.setId(id);
	
            
		List<SysScoreIdxVO> list  = this.sysScoreIdxService.searchIdxList(qc);
		SysScoreIdxVO vo =list.get(0);
	    List<KeyValueVO> optTypeList=this.getKvList(DictionaryConstant.CODE_SCORE_IDX_OPT_TYPE_CD);
	    List<KeyValueVO> oprList=this.getKvList(DictionaryConstant.CODE_SYS_OPR_CD);
		logger.info("加载SysScoreIdx Modify页面.");
		ModelAndView view = new ModelAndView("system/sysscoretmpl/sysScoreIdxAdd");
		view.addObject("idxId", id);
		view.addObject("tmplId", vo.getTmplCd());

		view.addObject("idxNm", vo.getIdxNm());
		view.addObject("idxDesc", vo.getIdxDesc());
		view.addObject("idxCd", vo.getIdxCd());
		view.addObject("optTypeList", JSON.toJSONString(optTypeList));
		view.addObject("oprList", JSON.toJSONString(oprList));
		view.addObject("detailFlag", detailFlag);
		return view;
	}
	

	/**
	 * 到模板新增页面
	 */
	@RequestMapping(value = "/toTmplAddPage.do", method = { RequestMethod.GET, RequestMethod.POST })
	public ModelAndView toAddPage(HttpServletRequest request, HttpServletResponse response) {

		logger.info("加载SysScoreTmpl Add页面.");
		ModelAndView view = new ModelAndView("system/sysscoretmpl/sysscoretmplAdd");
		view.addObject("detailFlag", true);
		return view;
	}
	
	@RequestMapping(value = "/toIdxAddPage.do", method = { RequestMethod.GET, RequestMethod.POST })
	public ModelAndView toIdxAddPage(HttpServletRequest request, HttpServletResponse response) {
		String id = request.getParameter("tmplId");
		logger.info("加载SysScoreIdx Add页面.");
		   List<KeyValueVO> optTypeList=this.getKvList(DictionaryConstant.CODE_SCORE_IDX_OPT_TYPE_CD);
		    List<KeyValueVO> oprList=this.getKvList(DictionaryConstant.CODE_SYS_OPR_CD);
		ModelAndView view = new ModelAndView("system/sysscoretmpl/sysScoreIdxAdd");
		view.addObject("tmplId", id);
		view.addObject("optTypeList", JSON.toJSONString(optTypeList));
		view.addObject("oprList", JSON.toJSONString(oprList));
		view.addObject("detailFlag", true);
		return view;
	}
	/**
	 * 模板初始化列表
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/initList.do", method = { RequestMethod.GET})
	@ResponseBody
	public Object initList(HttpServletRequest request, HttpServletResponse response) {
		String currentPage = request.getParameter("page");
		String pageSize = request.getParameter("pagesize");
		String orgCdLikeLeft=request.getParameter("buOrgCd");
		String tmplNm=request.getParameter("tmplNm");
		Pager page = new Pager();
		if (StringUtils.isNotEmpty(currentPage)) {
			page.setCurrentPage(Integer.parseInt(currentPage));
		}

		if (StringUtils.isNotEmpty(currentPage)) {
			page.setEveryPage(Integer.parseInt(pageSize));
		}

		SysScoreTmplQueryCondition qc = new SysScoreTmplQueryCondition();
		qc.setOrgCdLikeLeft(orgCdLikeLeft);
		qc.setTmplNm(tmplNm);
		page.setCondition(qc);
            
		Pager pager = this.sysScoreTmplService.searchListPage(page);
		List<SysScoreTmplDTO> list = (List<SysScoreTmplDTO>) pager.getResultList();
		if(list==null){
			list = new ArrayList<SysScoreTmplDTO>();
		}
		for (SysScoreTmplDTO sysScoreTmplDTO : list) {
			if(StringUtils.isEmpty(sysScoreTmplDTO.getOrgCd())) break;
			String orgName = this.sysOrgApiService.selectSysOrgNmByOrgCd(sysScoreTmplDTO.getOrgCd(),CurrentThreadContext.getFlServiceSysCd());
			sysScoreTmplDTO.setOrgNm(orgName);
		}
		List<SysScoreTmplVO> list1 =SysScoreTmplVOTransform.toVOList(list);
		ListPageVO<SysScoreTmplVO> listPage = new ListPageVO<SysScoreTmplVO>(list1, pager.getTotalCount());
		return listPage;
	}

	/**
	 * 模板指标初始化列表
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/initIdxList.do", method = { RequestMethod.GET})
	
	@ResponseBody
	public Object initIdxList(HttpServletRequest request, HttpServletResponse response) {
		String currentPage = request.getParameter("page");
		String pageSize = request.getParameter("pagesize");
		String tmplId=request.getParameter("tmplId");
		Pager page = new Pager();
		if (StringUtils.isNotEmpty(currentPage)) {
			page.setCurrentPage(Integer.parseInt(currentPage));
		}

		if (StringUtils.isNotEmpty(currentPage)) {
			page.setEveryPage(Integer.parseInt(pageSize));
		}

		SysScoreIdxQueryCondition qc = new SysScoreIdxQueryCondition();

		qc.setTmplCd(tmplId);
		page.setCondition(qc);
            
		List<SysScoreIdxVO> list  = this.sysScoreIdxService.searchIdxList(qc);

		//List<SysScoreIdxVO> list = (List<SysScoreIdxVO>) pager.getResultList();
		ListPageVO<SysScoreIdxVO> listPage = new ListPageVO<SysScoreIdxVO>(list, list.size());
		return listPage;
	}
	
	/**
	 * 模板指标选项初始化列表
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/initIdxOptList.do", method = { RequestMethod.GET})
	
	@ResponseBody
	public Object initIdxOptList(HttpServletRequest request, HttpServletResponse response) {
		String currentPage = request.getParameter("page");
		String pageSize = request.getParameter("pagesize");
		String idxId=request.getParameter("idxId");
		Pager page = new Pager();
		if (StringUtils.isNotEmpty(currentPage)) {
			page.setCurrentPage(Integer.parseInt(currentPage));
		}

		if (StringUtils.isNotEmpty(currentPage)) {
			page.setEveryPage(Integer.parseInt(pageSize));
		}

		SysScoreIdxOptQueryCondition qc = new SysScoreIdxOptQueryCondition();

		qc.setTmplIdxId(Long.parseLong(idxId));
		page.setCondition(qc);
            
		Pager pager = this.sysScoreIdxOptService.searchListPage(page);
		List<SysScoreIdxOptVO> list = (List<SysScoreIdxOptVO>) pager.getResultList();
		ListPageVO<SysScoreIdxOptVO> listPage = new ListPageVO<SysScoreIdxOptVO>(list, pager.getTotalCount());
		return listPage;
	}
	/**
	 * 删除模板数据
	 */
	@RequestMapping(value = "/removeTmplById.do", method = {RequestMethod.POST })
	@ResponseBody
	public DataResultVO removeById(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		String id = request.getParameter("id");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, "删除成功");
		if(StringUtils.isEmpty(id)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("参数不完整！");
			return result;
		}
		try {
			this.sysScoreTmplService.removeById(Long.valueOf(id));
			this.sysScoreIdxService.removeByTmplId(Long.valueOf(id));
			//SysScoreIdxQueryCondition qc1 = new SysScoreIdxQueryCondition();
			//qc1.setTmplCd(id);
			//List<SysScoreIdxVO> list =this.sysScoreIdxService.searchIdxList(qc1);
			
			//for(SysScoreIdxVO idxVo:list){
				
			//}
			logger.debug("==remove==id:" + id);
		}catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			logger.error(ex.getMessage(), ex);
		}catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("删除失败");
			logger.error("删除失败!", ex);
		}

		return result;
	}
	
	@RequestMapping(value = "/removeIdxById.do", method = {RequestMethod.POST })
	@ResponseBody
	public DataResultVO removeIdxById(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		String id = request.getParameter("id");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, "删除成功");
		if(StringUtils.isEmpty(id)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("参数不完整！");
			return result;
		}
		try {
			this.sysScoreIdxService.removeById(Long.valueOf(id));
			logger.debug("==remove==id:" + id);
		}catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			logger.error(ex.getMessage(), ex);
		}catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("删除失败");
			logger.error("删除失败!", ex);
		}

		return result;
	}

	/**
	 * 得到一条模板记录
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/getById.do", method = { RequestMethod.GET})
	@ResponseBody
	public Object getById(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		String id = request.getParameter("id");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, "获取成功");
		if(StringUtils.isEmpty(id)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("参数不完整！");
			return result;
		}
		
		try {
			SysScoreTmplVO vo = this.sysScoreTmplService.selectById(Long.valueOf(id));
			if(null == vo){
				result.setSuccess(BaseConstant.IS_YESNO_NO);
				result.setInfo("数据不存在！");
			}
			result.setData(vo);
		}catch(BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			logger.error(ex.getMessage(), ex);
		}catch(Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("获取数据失败！");
			logger.error("获取数据失败！", ex);
		}
		return result;
	}

	/**
	 * 新增模板
	 */
	@RequestMapping(value = "/Tmpladd.do", method = { RequestMethod.POST })
	@ResponseBody
	public Object add(HttpServletRequest request, HttpServletResponse response) throws BaseException {
	    String tmplId =request.getParameter("tmplId");
	    String tmplNm =request.getParameter("tmplNm");
		String orgCd = request.getParameter("orgCd");//组织机构ID
	    String tmplCd =request.getParameter("tmplCd");
	    String cons =request.getParameter("cons");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, "新增成功!");
		SysScoreTmplVO scoreTmpl=new SysScoreTmplVO();
		try {
			if(StringUtils.isNotBlank(tmplId)){
				//修改
              
				scoreTmpl.setId(Long.valueOf(tmplId));
				scoreTmpl.setTmplNm(tmplNm);
				scoreTmpl.setMdfTm(new Date());
				scoreTmpl.setOrgCd(orgCd);
				scoreTmpl.setTmplCd(tmplCd);
				scoreTmpl.setSysCd(CurrentThreadContext.getFlServiceSysCd());
				scoreTmpl.setCons(Float.parseFloat(cons));
				 this.sysScoreTmplService.modify(scoreTmpl);
				result.setSuccess(BaseConstant.IS_YESNO_YES);
				 result.setInfo("修改成功!");
				 result.setData(tmplId);
			}else{
				scoreTmpl.setTmplNm(tmplNm);
				scoreTmpl.setCrtTm(new Date());
				scoreTmpl.setOrgCd(orgCd);
				scoreTmpl.setTmplCd(tmplCd);
				scoreTmpl.setCons(Float.parseFloat(cons));
				scoreTmpl.setSysCd(CurrentThreadContext.getFlServiceSysCd());
				scoreTmpl.setIsDel(BaseConstant.IS_YESNO_NO);
				Long id = this.sysScoreTmplService.add(scoreTmpl);
				result.setData(id);
			}
		}catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
		}catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("新增失败!");
		}

		return result;
	}
	
	@RequestMapping(value = "/idxAdd.do", method = { RequestMethod.POST })
	@ResponseBody
	public Object idxAdd(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		String idxId = request.getParameter("idxId");
		String tmplId = request.getParameter("tmplId");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_ADD_SUCC);
		SysScoreIdxVO sysScoreIdxVO = null;
		Long id  =	 null;
		try {
			sysScoreIdxVO = getIdxRequestParams(request);
			if(StringUtils.isNotBlank(idxId)){
				sysScoreIdxVO.setId(Long.valueOf(idxId));
				sysScoreIdxVO.setTmplCd(tmplId);
				//修改
				this.sysScoreIdxService.modifyidxAndOpt(sysScoreIdxVO);
				id = Long.valueOf(idxId);
			}else{
				//添加
				sysScoreIdxVO.setTmplCd(tmplId);
				sysScoreIdxVO.setMdfTm(new Date());
				sysScoreIdxVO.setMdfUsrId(CurrentThreadContext.getCurrentUserId());
				sysScoreIdxVO.setCrtTm(new Date());
				sysScoreIdxVO.setCrtUsrId(CurrentThreadContext.getCurrentUserId());
				id = this.sysScoreIdxService.addidxAndOpt(sysScoreIdxVO);
			}
			
			result.setData(id);
			BaseLogUtils.info(logger, "add", "新增数据.[id="+id+"] DATA:" + JSONObject.toJSONString(sysScoreIdxVO), CurrentThreadContext.getCurrentUserName());
		}catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.info(logger, "add", "新增数据失败.DATA:" + JSONObject.toJSONString(sysScoreIdxVO) + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
			BaseLogUtils.error(logger, "add", "新增数据失败.DATA:" + JSONObject.toJSONString(sysScoreIdxVO) + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}

		return result;
	}
	
	private SysScoreIdxVO getIdxRequestParams(HttpServletRequest request) {
		SysScoreIdxVO sysScoreIdxVO = new SysScoreIdxVO();
		String idxId = request.getParameter("idxId");//模板指标ID
		String tmplId = request.getParameter("tmplId");//模板ID
		String idxNm = request.getParameter("idxNm");//指标名称
		String idxCd = request.getParameter("idxCd");//字段
		String idxDesc = request.getParameter("idxDesc");
		String detailList = request.getParameter("feeDetailList");//指标选项明细
		
		if(null!=idxId && !idxId.equals("")){
		sysScoreIdxVO.setId(Long.valueOf(idxId));
		}
		if(StringUtils.isNotBlank(idxId)){
			sysScoreIdxVO.setId(Long.parseLong(idxId));;
		}
		if(StringUtils.isNotBlank(tmplId)){
			sysScoreIdxVO.setTmplCd(tmplId);
		}
		if(StringUtils.isNotBlank(idxNm)){
			sysScoreIdxVO.setIdxNm(idxNm);
		}
		if(StringUtils.isNotBlank(idxCd)){
			sysScoreIdxVO.setIdxCd(idxCd);;
		}
		if(StringUtils.isNotBlank(idxDesc)){
			sysScoreIdxVO.setIdxDesc(idxDesc);
		}
		sysScoreIdxVO.setOrgCd("0000");
		List<SysScoreIdxOptVO> list = new ArrayList<SysScoreIdxOptVO>();
		if (StringUtils.isBlank(detailList)) {
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,"指标选项空!");
		}
		if (StringUtils.isNotBlank(detailList)) {
			list = JSON.parseArray(detailList, SysScoreIdxOptVO.class);
		}
		List<SysScoreIdxOptVO> list1 = new ArrayList<SysScoreIdxOptVO>();
		for (int i=0;i< list.size();i++) {
			SysScoreIdxOptVO sysScoreIdxOptVO=list.get(i);
			if(!sysScoreIdxOptVO.getOptTypCd().equals("1")){
				if(null==sysScoreIdxOptVO.getMaxVal()){
					throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,"上限值不能为空!");
				}
				if(null==sysScoreIdxOptVO.getMaxOpr() && sysScoreIdxOptVO.getOptTypCd().equals(2)){
					throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,"上限运算符不能为空!");
				}
				if(null==sysScoreIdxOptVO.getMixVal() && sysScoreIdxOptVO.getOptTypCd().equals(2)){
					throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,"下限值不能为空!");
				}
				if(null==sysScoreIdxOptVO.getMixOpr() && sysScoreIdxOptVO.getOptTypCd().equals(2)){
					throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,"下限运算符不能为空!");
				}
				if(null==sysScoreIdxOptVO.getOptDesc() ){
				//	throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,"选项描述不能为空!");
				}
				//if(null==sysScoreIdxOptVO.getScrVal()){
				//	throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,"分值不能为空!");
				//}
			}
				//sysScoreIdxOptVO.setTmplIdxId(Long.parseLong(idxId));
			list1.add(sysScoreIdxOptVO);
		}
		sysScoreIdxVO.setSysScoreIdxOptVOList(list1);;

		return sysScoreIdxVO;
	}

	/**
	 * 修改
	 */
	@RequestMapping(value = "/modify.do", method = { RequestMethod.POST })
	@ResponseBody
	public Object modify(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		String id = request.getParameter("id");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES,"修改成功!");
		if(StringUtils.isEmpty(id)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("参数不完整！");
			return result;
		}
		try {
			SysScoreTmplVO entity = new SysScoreTmplVO();
			entity.setId(Long.valueOf(id));
			boolean success = false;//this.sysScoreTmplService.modify(entity);
			if(!success){
				result.setSuccess(BaseConstant.IS_YESNO_NO);
				result.setInfo("修改失败！");
			}
		}catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			logger.error(ex.getMessage(), ex);
		}catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("修改失败!");
			logger.error("修改失败!", ex);
		}
		return result;
	}
	
	
	
	/*********************************业务模版页面使用方法*********************************/
	
	/**
	 * 根据系统代码、组织架构获取属于本架构下的所有评分模版信息,模版下面包含模版指标,指标下面包含模版项
	 * 本方法提供给模版页面使用
	 */
	@RequestMapping(value = "/searchScoreTmplList.do", method = { RequestMethod.GET })
	@ResponseBody
	public Object searchScoreTmplList(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES,"获取成功!");
		try {
			List<SysScoreTmplVO> sysScoreTmplVOList = this.sysScoreTmplService.searchScoreTmplList(CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd());
			if(CollectionUtils.isNotEmpty(sysScoreTmplVOList)){
				result.setData(sysScoreTmplVOList);
			}
		}catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			logger.info(ex.getMessage(), ex);
		}catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("修改失败!");
			logger.error("修改失败!", ex);
		}
		return result;
	}
	
	
	/**
	 *组织机构信息
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/searchOrgTree.do", method = { RequestMethod.GET})
	@ResponseBody
	public Object searchOrgTree(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, "获取成功");
		String cstMgrOrgCd = request.getParameter("cstMgrOrgCd");//客户经理ID
		try {
			JSONArray array = new JSONArray();
			Long currentOrgId = CurrentThreadContext.getCurrentOrgId();
			array = this.sysScoreTmplService.searchParentAndCurrentAllsOrgTree(currentOrgId,cstMgrOrgCd);
			//array = this.sysOrgService.searchOrgTreeByCurrentUser(CurrentThreadContext.getCurrentOrgCd(), CurrentThreadContext.getCurrentSysCd());
			if(null == array){
				array = new JSONArray();
			}
			result.setData(array);
		}catch(BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			logger.error(ex.getMessage(), ex);
		}catch(Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("获取数据失败！");
			logger.error("获取数据失败！", ex);
		}
		return result;
	}
	
	/**
	 * 检查模板名称是否存在
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/checktmplNm.do", method = { RequestMethod.POST})
	@ResponseBody
	public Object checktmplNm(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		String tmplNm = request.getParameter("tmplNm");//模板名称是否相同
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, "获取成功");
		if(StringUtils.isEmpty(tmplNm)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("参数不完整！");
			return result;
		}


			Pager page = new Pager();


			SysScoreTmplQueryCondition qc = new SysScoreTmplQueryCondition();
			qc.setTmplNmJQ(tmplNm);
			qc.setSysCd("0");
			page.setCondition(qc);
	            
	
		try {
			//查询名称是否存在
		//	Pager pager = this.sysScoreTmplService.searchListPage(page);
		//	List<SysScoreTmplVO> list = (List<SysScoreTmplVO>) pager.getResultList();	
		//	if(CollectionUtils.isEmpty(list)){
				result.setSuccess(BaseConstant.IS_YESNO_NO);
		//		result.setInfo("数据不存在！");
		//	}
		//	result.setData(list);
		}catch(BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			logger.error(ex.getMessage(), ex);
		}catch(Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("获取数据失败！");
			logger.error("获取数据失败！", ex);
		}
		return result;
	}
	
	private List<KeyValueVO> getKvList(String grpCd){
		List<KeyValueVO> kvList = new ArrayList<>();
		try {
			List<SysDictionaryDTO> sysDictionaryDTOList = this.sysDictionaryApiService.searchGroup(CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd(), grpCd);
			if(CollectionUtils.isNotEmpty(sysDictionaryDTOList)){
				for (SysDictionaryDTO sysDictionaryDTO : sysDictionaryDTOList) {
					KeyValueVO keyValueVO = new KeyValueVO();
					keyValueVO.setKey(sysDictionaryDTO.getCode());
					keyValueVO.setValue(sysDictionaryDTO.getCodeNm());
					kvList.add(keyValueVO);
				}
			}
		} catch (BaseException e) {
			BaseLogUtils.info(logger, "getKvList", "获取字典项组"+grpCd+"失败.ERROR:"+e.getMessage(), CurrentThreadContext.getCurrentUserName());
		} catch (Exception e) {
			BaseLogUtils.error(logger, "getKvList", "获取字典项组"+grpCd+"失败,系统错误.ERROR:"+e.getMessage(), CurrentThreadContext.getCurrentUserName());
		}
		return kvList;
	}
}

